package edu.nctu.csie.jichang.spring;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

/**
 * #{ database.databaseType=='POSTGRESQL' ? 'postgresql/ienc.xml' : 'mssql/ienc.xml'}
 */
public class Expression {

	private static final String SYMBOL_START     = "#{";
	private static final String SYMBOL_END       = "}";
	private static final String SYMBOL_QUESTION  = "?";
//	private static final String SYMBOL_COLON     = ":";
	private static final String SYMBOL_CONDITION = "(.+)([=><]{2})(.+)";
	private static final Pattern PATTERN = Pattern.compile(SYMBOL_CONDITION);
	
	private boolean isExpression;
	
	private String key;       //  database.databaseType
	private String condition; // ==
	private String target;    // 'POSTGRESQL'
	
	private String returnValue; // 'postgresql/ienc.xml' : 'mssql/ienc.xml'
	
	public Expression(){}
	
	public Expression(String pExpression){
		if(pExpression.indexOf(SYMBOL_START)>=0 && pExpression.lastIndexOf(SYMBOL_END)>=0){
			String tContent = pExpression.substring(pExpression.indexOf(SYMBOL_START)+SYMBOL_START.length(),pExpression.lastIndexOf(SYMBOL_END));
			tContent = tContent.trim();
			if(tContent.indexOf(SYMBOL_QUESTION)>=0){
				String tFront = tContent.substring(0,tContent.indexOf(SYMBOL_QUESTION)).trim();
				String tBack  = tContent.substring(tContent.indexOf(SYMBOL_QUESTION)+1).trim();
				Matcher tMatcher = PATTERN.matcher(tFront);
				if(tMatcher.find()) {
					setKey(tMatcher.group(1));
					setCondition(tMatcher.group(2));
					setTarget(tMatcher.group(3));
					setReturnValue(tBack);
					setExpression(true);
				}else{
					throw new RuntimeException("Can't parser format = " + pExpression);
				}
			}
		}else{
			setExpression(false);
			setReturnValue(pExpression);
		}
	}
	
	public String getValue(String pTarget){
		ExpressionParser tParser = new SpelExpressionParser();
		return tParser.parseExpression("'"+pTarget+"'"+condition+target+"?"+returnValue).getValue(String.class);
	}
	
	public boolean isExpression() {
		return isExpression;
	}
	public void setExpression(boolean isExpression) {
		this.isExpression = isExpression;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getReturnValue() {
		return returnValue;
	}
	public void setReturnValue(String returnValue) {
		this.returnValue = returnValue;
	}
	
//	public static void main(String[] args) {
//		Expression tExpression = new Expression("#{ database.databaseType == 'POSTGRESQL' ? 'postgresql/ienc.xml' : 'mssql/ienc.xml'}");
//		if(tExpression.isExpression()){
//			System.out.println(tExpression.getKey() +" " + tExpression.getValue("POSTGRESQL"));
//		}else{
//			System.out.println(tExpression.getReturnValue());
//		}
//	}

}